package frame

import (
	"bytes"

	"gitee.com/yanwc/ebicycle/device/internal/field"
	"gitee.com/yanwc/ebicycle/device/utils"

	gerrors "github.com/panjf2000/gnet/pkg/errors"
)

type FrameTag uint16

func (t *FrameTag) IndexOf(buf []byte) (idx int, err error) {
	b := utils.NumberToBytesWithBigEndian(*t)
	idx = bytes.IndexAny(buf, string(b))
	if idx < 0 {
		return -1, gerrors.ErrIncompletePacket
	}

	return
}

type IFrame interface {
	WrapPacket(data []byte) (out []byte)
	UnWrapPacket(data []byte) error
	RawData() []byte
	PayloadData() []byte           // Payload
	GetSN() field.SerialNumber     // 消息序列号
	GetCmdWord() field.CommandWord // 命令字
	CRCCheck() bool                // 帧校验
}
